package com.guigu.controller;

import com.guigu.CommodityService;
import com.guigu.ProcureService;
import com.guigu.ProcureDetailsService;
import com.guigu.pojo.Commodity;
import com.guigu.pojo.Procure;
import com.guigu.pojo.ProductImage;
import com.guigu.pojo.Quote;
import com.guigu.utils.NumberUtils;
import com.guigu.utils.NumberUtils1;
import com.guigu.vo.Pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

@CrossOrigin
@RestController
@RequestMapping("Commodity")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private NumberUtils1 numberUtils;
    @Autowired
    private ProcureService procureService;
@Autowired
private ProcureDetailsService procure_detailsService;


    //添加商品
    @RequestMapping("insert")
    public int insert(@RequestBody Commodity commodity){
        commodity.setStatus(0);
        commodity.setStoreNum(10);
        commodity.setDeleteTag("C001-1");
        System.out.println("Commodity"+commodity);
        boolean save = commodityService.save(commodity);
        //查询最大id
        int id = commodityService.selectMaxId();
        System.out.println(id);
        return id;
    }
    @RequestMapping("insertImage")
    public int insertImage(@RequestBody ProductImage productImage){
        int i = commodityService.insertImage(productImage);
        return i;
    }
//分页查询所有商品
    @RequestMapping("selectAll/{pageNum}/{pageSize}")
    public Pojo<Commodity> selectAll(@PathVariable int pageNum,@PathVariable int pageSize){
        System.out.println("CommodityController.selectAll");
        Pojo<Commodity> commodityPojo = commodityService.selectAll(pageNum, pageSize);
        return commodityPojo;
    }
    @RequestMapping("selectPrice/{pageNum}/{pageSize}")
    public Pojo<Quote> selectPrice(int id, @PathVariable int pageNum, @PathVariable int pageSize){
        System.out.println("CommodityController.selectPrice");
        System.out.println(id);
        System.out.println(pageNum+" "+pageSize);
        Pojo<Quote> commodityPojo = commodityService.selectPrice(id, pageNum, pageSize);
        System.out.println(commodityPojo);
        return commodityPojo;
    }
    @PostMapping("/procureProcureDetailsAdd")
    public String procureProcureDetailsAdd(@RequestBody com.guigu.vo.ProcureProcureDetails procureProcureDetails){
        System.out.println("iiiii"+procureProcureDetails);

// 创建map集合结构
        Map<Integer, List<com.guigu.pojo.ProcureDetails>> map = new HashMap<>();
        for (com.guigu.pojo.ProcureDetails procure_details : procureProcureDetails.getDetails()) {
// 获取当前采购明细的供货商id
            Integer vendorId = procure_details.getVendorId();
// 判断map中是否存在该供货商id
            if (map.containsKey(vendorId)) {
// 存在，获取该供货商的采购明细
                List<com.guigu.pojo.ProcureDetails> procureDetailsList = map.get(vendorId);
// 将当前采购明细添加到该供货商的采购明细中
                procureDetailsList.add(procure_details);
            } else {
// 不存在，创建该供货商的采购明细
                List<com.guigu.pojo.ProcureDetails> procureDetailsList = new ArrayList<>();
                procureDetailsList.add(procure_details);
// 将该供货商的采购明细添加到map中
                map.put(vendorId, procureDetailsList);
            }
        }


// 遍历map键值对，完成对采购单表和采购明细表的添加
        for (Map.Entry<Integer, List<com.guigu.pojo.ProcureDetails>> entry : map.entrySet()) {
// 获取当前供货商的id
            Integer key = entry.getKey();
// 当前供货商的采购明细
            List<com.guigu.pojo.ProcureDetails> value = entry.getValue();
// 向采购单表插入记录
            Procure procure = new Procure();
// 生成采购单有规则的编号 1002025040300001
            String procureNumber = numberUtils.generateDocumentId("100");
            System.out.println("ppppp"+procureNumber);
            procure.setProcureNumber(procureNumber);

// 设置采购单的供货商id
            procure.setVendorId(key);
// 采购单的采购日期
            procure.setProcureTime(new Date());
// 采购单的备注
            procure.setProcureRemark(procureProcureDetails.getProcureRemark());
// 采购人的id
            procure.setPurchasers(procureProcureDetails.getProcureId());
//待入库
            procure.setGatherTag("K002-1");
// 设置采购单的采购状态
            procure.setProcureTag(1);
// 审核状态
            procure.setCheckTag("S001-0");
// 设置采购单的总数量和总金额
            Integer totalCount = 0;
            BigDecimal totalPrice = BigDecimal.ZERO; // 初始化为0

            for (com.guigu.pojo.ProcureDetails detail : value) {
                totalCount += detail.getCount();
                totalPrice = totalPrice.add(
                        new BigDecimal(detail.getSubtotal().toString()) // 确保转为BigDecimal
                );
            }

            procure.setProcureCount(totalCount);
            procure.setProcurePrice(totalPrice); // 设置总金额
// 保存采购单
            procureService.save(procure);
// 获取到新插入的采购单id
            Integer id = procure.getId();

// 循环插入采购单明细
            for (int i = 0; i < value.size(); i++) {
                com.guigu.pojo.ProcureDetails details = value.get(i);
// 设置明细所属的采购单id
                details.setParentId(id);
                procure_detailsService.save(details);
            }
        }
        return "ok";
    }
}